$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + '/auth/userList',
		datatype: "json",
		colModel: [			
		{ label: '用户ID', name: 'id', index: "user_id", width: 45, key: true },
		{ label: '用户名', name: 'username', width: 75 },
		{ label: '邮箱', name: 'email', width: 70 },
		{ label: '手机号', name: 'mobile', width: 100 },
		{ label: '角色', name: 'roleCode', width: 100, formatter: function(value, options, row){
			return value
			.replace(/[0][0][0][0]/g, '超级管理员')
			.replace(/[0][0][0][1]/g, '系统管理员')
			.replace(/[0][0][0][2]/g, '商家')
			.replace(/[0][0][0][3]/g, '财务')
			.replace(/[0][0][0][4]/g, '试客');
		} },
		{ label: '状态', name: 'status', width: 50, formatter: function(value, options, row){
			return value === 0 ? 
			'<span class="label label-danger">禁用</span>' : 
			'<span class="label label-success">正常</span>';
		}},
		{ label: '创建时间', name: 'createTime', index: "create_time", width: 80,formatter: function(value, options, row){
                return  parseTime(value);
            }}
		],
		viewrecords: true,
		height: 555,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true, 
		rownumWidth: 25, 
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
            root: "data.dataList",
            page: "data.currentPage",
            records: "data.totalNum",
            total: "data.total"
		},
		prmNames : {
			page:"pageNo", 
			rows:"pageSize", 
			order: "order"
		},
		gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			username: null
		},
		showList: true,
		title:null,
		roleList:{},
		user:{
			status:1,
			roleIdList:[]
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.roleList = {};
			vm.user = {status:1,roleIdList:[]};
			
			//获取角色信息
			this.getRoleList();
		},
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			
			vm.showList = false;
			vm.title = "修改";
			
			vm.getUser(userId);
			//获取角色信息
			this.getRoleList();
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "auth/user/delete",
					contentType: "application/json",
					data: JSON.stringify(userIds),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			if(vm.validator()){
				return ;
			}

			var url = vm.user.id == null ? "/auth/user/save" : "/auth/user/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.user),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getUser: function(userId){
			$.get(baseURL + "/auth/get?userId="+userId, function(r){
				vm.user = r.data;
				vm.user.password = null;
			});
		},
		getRoleList: function(){
			$.get(baseURL + "/auth/role", function(r){
				vm.roleList = r.list;
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'keyword': vm.q.keyword},
				page:page
			}).trigger("reloadGrid");
		},
		validator: function () {
			if(isBlank(vm.user.username)){
				alert("用户名不能为空");
				return true;
			}

			if(vm.user.userId == null && isBlank(vm.user.password)){
				alert("密码不能为空");
				return true;
			}

			if(isBlank(vm.user.email)){
				alert("邮箱不能为空");
				return true;
			}

			if(!validator.isEmail(vm.user.email)){
				alert("邮箱格式不正确");
				return true;
			}
		}
	}
});